package com.inhim.pjhim.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.inhim.pjhim.R;
import com.inhim.pjhim.entity.ReaderBean;
import com.inhim.pjhim.utils.GlideCircleUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;


public class SearchClassfiyAdapter extends RecyclerView.Adapter<SearchClassfiyAdapter.ViewHolder> {
    private List<ReaderBean.TypeListBean> listBeans = new ArrayList<>();
    private Context context;

    public SearchClassfiyAdapter(Context context) {
        this.context = context;
    }

    public void setListBeans(List<ReaderBean.TypeListBean> listBeans){
        this.listBeans = listBeans;
        notifyDataSetChanged();
    }
    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_search_class, viewGroup, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int i) {
        ReaderBean.TypeListBean data =  listBeans.get(i);
        holder.tv_class.setText(data.getName());
        Glide.with(context).load(data.getIcon())
                .transform(new CenterCrop(context),new GlideCircleUtils.GlideRoundTransform(context, 20))
                .into(holder.ivClass);
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    listener.onItemClick(data);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return listBeans.size();
    }


//    public class PersonViewHolder extends BaseViewHolder<ReaderBean.TypeListBean> {
//        public TextView tv_class;
//        public ImageView ivClass;
//
//        public PersonViewHolder(View itemView) {
//            super(itemView);
//            tv_class = getView(R.id.tv_class);
//            ivClass = getView(R.id.ivClass);
//        }
//
//        @Override
//        public void setData(ReaderBean.TypeListBean data) {
//            super.setData(data);
//
//            tv_class.setText(data.getName());
//            Glide.with(getContext()).load(data.getIcon())
//                    .transform(new GlideCircleUtils.GlideRoundTransform(getContext(), 8)).into(ivClass);
//           /* if(viewHolder!=null){
//                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Intent intent=new Intent(context, TypeListActivity.class);
//                        intent.putExtra("TAG","ReadingTwoFragment");
//                        intent.putExtra("ReaderTypeList", (Serializable) getAllData());
//                        intent.putExtra("position",getAdapterPosition());
//                        context.startActivity(intent);
//                    }
//                });
//            }*/
//        }
//
//    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView tv_class;
        public ImageView ivClass;
        public View rootView;
        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            rootView = itemView;
            tv_class = itemView.findViewById(R.id.tv_class);
            ivClass = itemView.findViewById(R.id.ivClass);
        }
    }
    private ItemClickListener listener;

    public void setListener(ItemClickListener listener) {
        this.listener = listener;
    }

    public interface ItemClickListener {
        void onItemClick(ReaderBean.TypeListBean data);
    }
}
